package com.example.books.ui.dashboard;

import android.content.SharedPreferences;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.books.MainActivity;
import com.example.books.data.Book;
import com.example.books.data.Collection;

import org.litepal.LitePal;

import java.util.List;

public class DashboardViewModel extends ViewModel {
    private int sid;

    public void setSid(int sid) {
        this.sid = sid;
    }

    public DashboardViewModel() {
    }

    /**
     * 收藏图书数据
     * @return
     */
    public List<Book> bookList(){
        List<Book> all = LitePal.findAll(Book.class, cB());
        return all;
    }

    /**
     *
     * @return 收藏id组
     */
    private long[] cB(){
        List<Collection> collections = LitePal.where("userId=?",""+sid).find(Collection.class);
        long[] ids = new long[collections.size()];
        int i=0;
        for (Collection collection:collections) {
            long bookId = collection.getBookId();
            ids[i] = bookId;
            i++;
        }
        return ids;
    }
}